import { reactive } from 'vue';
import { getExcels } from '@/api/url/base';
import { formatDate, isJSON, isNullPm } from '@/utils/baseMethods';
import notification from '@arco-design/web-vue/es/notification';

export default function importConfigFunction(_props: any, option: any) {
  const { emit } = option;
  const base = reactive<any>({
    fileList: [],
    steamList: [],
  });

  /**
   * @function customReques
   * @description 自定义上传行为
   */
  const customReques = (_option: any, _type: any) => {
    const { fileItem } = _option;
    base.steamList.push(fileItem);
    if (base.fileList.length > 0) {
      base.fileList.map((res: any, index: any) => {
        if (isNullPm(res.url)) {
          base.fileList.splice(index, 1);
        }
      });
    } else {
      base.fileList = [];
    }
    base.steamList.map((res: any, index: any) => {
      base.fileList.push({
        name: res.name,
        url: res.url,
        uid: res.uid,
        file: res.file,
      });
    });
    const result = base.fileList.reduce(function (prev, current) {
      const exists = prev.some(function (item) {
        return item.name === current.name;
      });
      if (!exists) {
        prev.push(current);
      }
      return prev;
    }, []);
    base.fileList = result;

    getUpdata();
  };

  /**
   * @exports getUpdata 点击上传文件
   * @param param
   */
  const getUpdata = () => {
    notification.info('文件上传中,请耐心等待~');
    const formData = new FormData();
    base.fileList.map((res: any) => {
      if (!isNullPm(res.file)) formData.append('files', res.file);
    });
    const staem: any = null;
    base.fileList.map((res: any) => {
      if (isNullPm(res.file)) sisn++;
    });

    getExcels(formData, { responseType: 'blob'}).then((res: any) => {
      const steamuiio = false;
      const reader = new FileReader();
      reader.onload = function (e: any) {
        let result=JSON.parse(e.target.result);
        if (result.code!=200) {
          notification.error({
            title: '程序发现错误!',
            content: result.message,
            duration: 10000,
          });
        } else {
          notification.success('导入excel成功');
          // const link = document.createElement('a');
          // const blob = new Blob([res], { type: 'application/vnd.ms-excel' });
          // link.style.display = 'none';
          // link.href = URL.createObjectURL(blob);
          // link.download = `返回结果${formatDate(new Date(), 'yyyyMMddHHmm')}.xlsx`; //下载的文件名
          // document.body.appendChild(link);
          // link.click();
          // document.body.removeChild(link);
          location.reload();
        }
      };
      reader.readAsText(res);
    });
  };

  /**
   * @function beforeRemove
   * @description 删除图片
   * @param fileItem
   */
  const beforeRemove = (fileItem) => {
    const index = base.fileList.findIndex((item: any) => item.url == fileItem.url);
    base.fileList.splice(index, 1);
    emit('filelist', base.fileList);
  };

  return {
    base,
    customReques,
    beforeRemove,
    getUpdata,
  };
}
